package com.yida.common.core.utils.regular;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hmh
 * @date 2022/10/26
 */
public class ReUtils {

    /**
     * 给定正则返回校验
     * 
     * hmh 20221026
     * 
     * @param content
     *            待校验值
     * @param regex
     *            正则
     * @return
     */
    public static Matcher getMatcher(String content, String regex) {
        return re(content, regex);
    }

    /**
     * 校验手机号是否合法
     * 
     * hmh 20221026
     * 
     * @param phonenumber
     *            手机号
     * @return
     */
    public static boolean checkPhone(String phonenumber) {
        String regex = "^1[3|4|5|6|7|8|9][0-9]\\d{8}$";
        Matcher matcher = re(phonenumber, regex);
        return isTrue(matcher);
    }

    /**
     * 校验是否纯数字
     * 
     * hmh 20221026
     * 
     * @param num
     *            数字
     * @return
     */
    public static boolean checkIsNum(String num) {
        String regex = "(^[\\-0-9][0-9]*(.[0-9]+)?)$";
        Matcher matcher = re(num, regex);
        return isTrue(matcher);
    }

    /**
     * 获取正则判断主体
     * 
     * hmh 20221026
     * 
     * @param content
     *            待校验值
     * @param regex
     *            正则
     * @return
     */
    private static Matcher re(String content, String regex) {
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(content);
        return matcher;
    }
    
    /**
     * 是否成功
     * @param matcher 正则主体
     * @return
     */
    private static boolean isTrue(Matcher matcher) {
        if (matcher.find()) {
            return true;
        }
        return false;
    }
    
    public static void main(String[] args) {
        String i="11a";
        System.out.println(ReUtils.checkIsNum(i));
    }
}
